package org.casper.web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.casper.bean.User;
import org.casper.dao.UserDao;

/**
 * Servlet implementation class IndexServlet
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao userDao = new UserDao();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int rows = 2;
		int offset =0;
		if(request.getParameter("offset")==null){
			offset = 0;
		}else{
			offset = Integer.parseInt(request.getParameter("offset"));
		}
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		//查询全部的用户
		//ArrayList<User> allUsers = userDao.selectAllUsers();
	
		session.setAttribute("offset", offset);
		session.setAttribute("rows", rows);
		ArrayList<User> allUsers = userDao.selectUserByPage(offset*rows, rows);
		session.setAttribute("allUsers", allUsers);
		
		//查询总记录数
		int count = userDao.selectCount();
		session.setAttribute("count", count);
		//总页数
		session.setAttribute("pages", count/rows);
		
		//request.getRequestDispatcher("index.jsp").forward(request, response);
		request.getRequestDispatcher("pageIndex.jsp").forward(request, response);
	}
       
	
}
